package com.luokangtao.common.util;

import com.alibaba.fastjson.JSONObject;
import com.luokangtao.common.entity.User;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;

/**
 * 用户工具类
 * @author luokangtao
 * @create 2019-07-25 10:27
 */
@Slf4j
public class UserUtil {

    //获取当前登陆用户信息
    public static User currentUser(){
        User user = new User();
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String authorization = request.getHeader("Authorization");
        if(authorization!=null){
            //解析token
            Claims claims = new JwtUtil().parseJWT(authorization);
            //获取实体信息
            String subject = claims.getSubject();
            user = JSONObject.parseObject(subject, User.class);
            log.info("解析出来的数据:{}",user);
        }
        return user;
    }

}
